home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14397 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  50 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How do I change the middle of a file?
  5. Date: 30 Mar 1996 02:23:02 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4ji5u6$eu0@dub-news-svc-3.compuserve.com>
  8. NNTP-Posting-Host: hd75-001.compuserve.com
  9.  
  10. culbreth@cc.gatech.edu (Matthew W. Culbreth) s'Θcrit :
  11. > Howdy,
  12. > I've got a big text file that acts as a database.  I need to be able
  13. > to search through the file, find a particular line, and change it.
  14. > I've tried opening the file in (ios::in|ios::app), but now the program
  15. > won't getline().
  16. > Any ideas?
  17. > Thanks,
  18. > Matt
  19. > -- 
  20. >                     
  21. >             -Matthew W. Culbreth (culbreth@cc.gatech.edu)
  22. >                 -CS student
  23. >                 -Georgia Tech, College of Computing
  24. Text files are not structured, and have varying length records.
  25. So you cannot use them to change a line on the same place.
  26. The best thing to do is to recreate another text file with the
  27. altered line, then deleting the old one, and renaming the new one.
  28. If this copy is judged long, don't use varying length records.
  29.  
  30. Open your file in binary mode, and store fixed length records,
  31. so that you can seek back to the beginning of the record you
  32. want to alter and that you have just read, prior to writing it
  33. safely in a place with enough space for your new record.
  34.  
  35. You can also modify the structure of you file, by blanking
  36. lines you want to replace, and adding your new records at the
  37. first blank line which is long enough to store your new string.
  38. The blanking status of a line can be found on its first character
  39. so that you can count the length until the neline character, then
  40. see if it is also a blank line (then rewrite the newline with
  41. a blank). Don't forget: a NewLine under DOS is a couple of characters
  42. even though you get only one char using getc() on a text file.
  43.  
  44.